/***************************************************************************
 *   Copyright (C) 2007 by Jiří Němec   *
 *   nemec.jiri@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <QTranslator>
#include <QStatusBar>
#include <QIcon>
#include <QDir>
#include <QtGui>
#include "slovniq.h"
#include "hledani.h"


Slovniq::Slovniq() {

    QCoreApplication::setOrganizationName ( "Jiri Nemec" );
    QCoreApplication::setOrganizationDomain ( "nemcovi.eu" );
    QCoreApplication::setApplicationName ( "slovniQ" );
    this->slovnik = new HashTabulka;

    nastaveni = new QSettings(QSettings::IniFormat, QSettings::UserScope,                    QCoreApplication::applicationName(),QCoreApplication::organizationName());
    nastaveni->beginGroup("MainWindow");
    QPoint pos = nastaveni->value ( "pos", QPoint ( -1, -1 ) ).toPoint();
    QSize size = nastaveni->value ( "size", QSize ( 260, 350 ) ).toSize();
    nastaveni->endGroup();
    resize ( size );

    if (pos.rx() >= 0 && pos.ry() >= 0) move ( pos );
    setWindowIcon(QIcon(":/res/icon.png"));

    setWindowTitle ( "slovniQ" );
    QWidget *okno = new QWidget;
    hledaSe = new QLineEdit ( "" );
    vystup = new QTextBrowser;
    vystup->setOpenLinks(false);
    jazyk = new QComboBox;
    jazyk->addItem("en");
    jazyk->addItem("cs");
    stav = new QStatusBar;
    vystup->setHtml(QString::fromUtf8("<html><head></head><body><center><table><tr><td><img src=\":res/icon.png\" /></td><td style=\"font-size:32px;\" >slovniQ</td></tr></table><p> autor: Jiří Němec &lt;nemec.jiri@gmail.com></p><p>Slovníková data jsou poskytuje<br /> GNU/FDL Anglicko-Český slovník <br /> http://slovnik.zcu.cz/</p></center></body></html>"));

    // GUI

    vertikalne = new QVBoxLayout;
    horizontalne = new QHBoxLayout;

    horizontalne->addWidget ( hledaSe );
    horizontalne->addWidget ( jazyk );
    vertikalne->addLayout ( horizontalne );
    vertikalne->addWidget ( vystup );
    vertikalne->addWidget ( stav );

    okno->setLayout ( vertikalne );

    connect ( hledaSe, SIGNAL ( returnPressed () ), this, SLOT ( hledatUrcite() ) );
    connect ( hledaSe, SIGNAL ( textChanged ( QString ) ), this, SLOT ( hledatZive ( QString ) ) );
    connect( jazyk, SIGNAL(  currentIndexChanged(QString) ),  this, SLOT ( hledatUrcite() ) );
    slovnikData = new SlovnikData(this->slovnik,QFileInfo(nastaveni->fileName()).absolutePath());
    vlaknoHledani = new Hledani(this->slovnik,QFileInfo(nastaveni->fileName()).absolutePath());

    connect ( slovnikData, SIGNAL ( zmenaStatusu(QString) ), this, SLOT ( zmenaStatusu(QString)) );
    qRegisterMetaType<Radek>("Radek");
    qRegisterMetaType<QVector<Radek> >("QVector<Radek>");
    connect ( vlaknoHledani, SIGNAL ( vysledekHledani ( QVector<Radek> ) ), this, SLOT ( vysledekHledani ( QVector<Radek> ) ) );
    connect (this , SIGNAL ( startHledani (QString ) ), vlaknoHledani, SLOT ( startHledani(QString) ) );
    connect (this , SIGNAL ( stopHledani () ), vlaknoHledani, SLOT ( setStop() ) );
    connect (vystup, SIGNAL (highlighted(const QString)),SLOT( linkHovered(const QString) ));
    connect (vystup, SIGNAL (anchorClicked(QUrl)),SLOT( webHandler() ));
    setCentralWidget ( okno );
    slovnikData->start();
    vlaknoHledani->start();

}

void Slovniq::hledatUrcite () {
    this->hledatUrciteB = true;
    hledani ( hledaSe->text() );
}

void Slovniq::hledatZive ( const QString & text ) {
    this->hledatUrciteB = false;
    hledani ( text );
}

void Slovniq::zmenaStatusu( const QString & text ) {
    stav->showMessage (text);
}

void Slovniq::hledani ( const QString & text ) {
    this->hledany = text;
    vlaknoHledani->stop = true;
    vlaknoHledani->wait();
    vlaknoHledani->setText ( text );
    vlaknoHledani->setPriority(jazyk->currentText());
    vlaknoHledani->start();

}

void Slovniq::linkHovered(const QString& text) {
    this->oznacene = text;
}

void Slovniq::webHandler() {
    vystup->setHtml(this->vlaknoHledani->najdiPodlePozice(this->oznacene.toLongLong()).doHtml());
}

void Slovniq::vysledekHledani ( const QVector<Radek> & v ) {
    QString ret("<html><head><style type=\"text/css\">.cs{color: red} .en{color: blue} a{text-decoration:none}a:hover{text-decoration:underline}</style></head><body>");
    int pocet = v.count();
    stav->showMessage (QString::number(pocet) + QString::fromUtf8( " nalezených slov"));
    foreach(Radek radek, v) {
        if (this->hledatUrciteB || pocet <= 100 || ((radek.anglicky == this->hledany) ||  (radek.cesky == this->hledany)))
            ret.append("<a href=\"" +  QString::number(radek.position) + "\" ><span class=\"en\" >" + radek.anglicky + "</span> - <span class=\"cs\" > " + radek.cesky + "</span></a> " + radek.poznamky + " " + radek.specPoznamky +  "<br />");
    }

    ret.append(QString::fromUtf8("</body></html>"));
    this->vystup->setHtml(ret);
}

Slovniq::~Slovniq() {
    delete this->slovnik;
    nastaveni->beginGroup("MainWindow");
    nastaveni->setValue("size", size());
    nastaveni->setValue("pos", pos());
    nastaveni->endGroup();
    delete nastaveni;
    delete hledaSe;
    delete vystup;
    delete jazyk;
    delete stav;
}

